/**
 * 
 * 广州锐鹏计算机科技有限公司（www.ruipengkj.com）
 * 
 * 
 * Copyright (c) 2011-2012 RUIPENGKJ.Co.Ltd. All rights reserved.
 * 
 * */
package com.ruipengkj.view.memberView;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;
import javax.swing.table.TableRowSorter;

import com.ruipengkj.ApplicationContext;
import com.ruipengkj.CardComponentListener;
import com.ruipengkj.bean.TUser;
import com.ruipengkj.commons.BuildSpringApplication;
import com.ruipengkj.commons.base.query.PageIndex;
import com.ruipengkj.commons.base.query.PageModel;
import com.ruipengkj.commons.base.query.PageView;
import com.ruipengkj.commons.util.TabelUtil;
import com.ruipengkj.commons.util.jna.QueryMember;
import com.ruipengkj.component.DataPackageComponent;
import com.ruipengkj.component.JLabelJTextFieldComponent;
import com.ruipengkj.component.RPJButtonBlue;
import com.ruipengkj.component.RPJButtonGray;
import com.ruipengkj.component.jpanel.QueryPanel;
import com.ruipengkj.component.jtable.RPTable;
import com.ruipengkj.component.page.PagingComponent;
import com.ruipengkj.factory.ImageFactory;
import com.ruipengkj.model.MemberQueryCardModel;
import com.ruipengkj.service.TUserService;
import com.ruipengkj.view.base.BasePagePanel;

/**
 * 说明：
 * 会员卡片查询UI
 * @Author：黄盛强
 * @Date：2012-03-20
 */
public class MemberQueryCardPanel extends BasePagePanel<TUser> implements Runnable, QueryMember{

	private static final long serialVersionUID = 1L;
	
	private JLabelJTextFieldComponent cardNumTF = null; //卡号
	private JLabelJTextFieldComponent nameTF = null;//姓名
	private JLabelJTextFieldComponent tel = null;//电话
	private DataPackageComponent startTimeTF = null;//发卡时间
	private DataPackageComponent timeTF = null;//时间
	
	
	private JButton queryButton = null;//查询按钮
	private JButton cleateButton = null;//清空按钮

	private RPTable table;
	private MemberQueryCardModel memberQueryCardModel;
	private PagingComponent pagingComponent;//翻页组件
	private PageView<TUser> pageView;//翻页主键
	private JScrollPane tableJsp;
	private TUserService tUserService;	
	private MenberDetailDialog menberDetailDialog;//会员详细窗体
	public MemberQueryCardPanel() {
		tUserService=BuildSpringApplication.getInstance().getTUserServiceImpl();
		this.setLayout(new BorderLayout());
		this.init();
		this.initListener();
		ApplicationContext.listenCard(cardNumTF.getText(), ApplicationContext.CardType.CT_DLSended);
		ApplicationContext.query_member = this;
		cardNumTF.getText().addComponentListener(new CardComponentListener());
	}
	
	private void init() {
		pageView=new PageView<TUser>();
		memberQueryCardModel=new MemberQueryCardModel();
		table=uiFactory.createRPJTable(memberQueryCardModel, null, this, 30, JLabel.CENTER);
//		table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
		tableJsp=uiFactory.createJScrollPane(table);
		pagingComponent=new PagingComponent();
		pagingComponent.setPageDelegale(this);
		
		this.add(this.topPanel(), BorderLayout.NORTH);
		this.add(tableJsp, BorderLayout.CENTER);
		this.add(pagingComponent, BorderLayout.SOUTH);
		//排序
		TableRowSorter<MemberQueryCardModel> tableRowSorter = new TableRowSorter<MemberQueryCardModel>(memberQueryCardModel);  
		table.setRowSorter(tableRowSorter);
	}
	
	private JPanel topPanel(){
		QueryPanel queryPanel=new QueryPanel("会员卡查询",ImageFactory.loadImage(ImageFactory.FUN_2_ICON_3));
		JPanel jp=new JPanel(new FlowLayout(FlowLayout.LEFT));
		tel = new JLabelJTextFieldComponent("手机：", 15);
		cardNumTF = new JLabelJTextFieldComponent("卡号：", 15);
		nameTF = new JLabelJTextFieldComponent("姓名：", 15);
		startTimeTF = new DataPackageComponent("发卡时间：", 15);
		timeTF = new DataPackageComponent("时间：", 15);
		queryButton = new RPJButtonBlue("查询");
		cleateButton= new RPJButtonGray("清空");
		jp.add(cardNumTF);
		jp.add(nameTF);
		jp.add(tel);
		jp.add(startTimeTF);
		jp.add(timeTF);
		jp.add(queryButton);
		jp.add(cleateButton);
		queryPanel.addPanel(jp);
//		queryPanel.addBoxPanel(jp);
		return queryPanel;
	}

	private void initListener(){
		queryButton.addActionListener(this);
		cleateButton.addActionListener(this);
		nameTF.getText().addKeyListener(this);
		tel.getText().addKeyListener(this);
	}
	@Override
	public void run() {
		this.termQuery(1);
	}
	//条件查询
	protected void termQuery(final int currentPage) {
		final TUser tuser=new TUser();
		tuser.setCardNum(cardNumTF.getText().getText().trim());
		tuser.setRealName(nameTF.getText().getText().trim());
		tuser.setSendCardTime(startTimeTF.getDate());
		tuser.setMobile(tel.getText().getText());
		tuser.setEndTime(timeTF.getDate());
		new Thread(){
			public void run(){
				tableJsp.getViewport().remove(table);
				tableJsp.getViewport().add(waitComponent);
				try {
					pageView.setCurrentPage(currentPage);
					PageModel<TUser> pageModel=tUserService.getScrollService(pageView.getFirstResult(), pageView.getMaxResult(),tuser);
					pageView.setPageModel(pageModel);
					pageView.setPageIndex(PageIndex.getPageIndex(pageView.getPageCode(), pageView.getCurrentPage(), pageView.getTotalPage()));
					if(pageModel!=null){
						TabelUtil.clearTableRowSorter(table);
						memberQueryCardModel.setRowData(pageModel.getResultList());
						SwingUtilities.invokeLater(new Runnable() {
							@Override
							public void run() {
								tableJsp.getViewport().remove(waitComponent);
								tableJsp.getViewport().add(table);
								TabelUtil.setJTabelAutoClumnWidth(table);
								pagingComponent.show();
							}
						});
					}else{
						//没有订单数据
						tableJsp.getViewport().remove(waitComponent);
						tableJsp.getViewport().add(table);
						memberQueryCardModel.clear();
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}.start();
	}
	@Override
	protected PageView<TUser> getPageView() {
		return pageView;
	}
	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource()==queryButton){
			//查询
			this.termQuery(1);
		}else if(e.getSource()==cleateButton){
			//清除
			cardNumTF.getText().setText("");
			nameTF.getText().setText("");
			startTimeTF.setDate(null);
			timeTF.setDate(null);
		}
	}
	
	@Override
	public void keyReleased(KeyEvent e) {
		if(e.getKeyCode()==KeyEvent.VK_ENTER){
			if(e.getSource()==nameTF.getText()||e.getSource() == tel.getText()){
				//查询会员
				queryButton.doClick();
			}
		}
	}
	@Override
	public void mouseClicked(MouseEvent e) {
    	if(e.getSource()==table&&e.getClickCount() == 2){
    		//表格
    		String id=table.getValueAt(table.getSelectedRow(), 0).toString();
    		try {
				TUser user=tUserService.getByIdService(id);
				if(user==null){
					JOptionPane.showMessageDialog(null, "当前会员不存在");
					return;
				}
				menberDetailDialog=new MenberDetailDialog(null, "会员详细", true);
	    		menberDetailDialog.showDialog(user);
			} catch (Exception e1) {
				e1.printStackTrace();
			}
    	}
    }

	@Override
	public void clear() {
		memberQueryCardModel.clear();
		cardNumTF.getText().setText(""); //卡号
		nameTF.getText().setText("");;//姓名
		tel .getText().setText("");;//电话
		startTimeTF.setDate(null);//发卡时间
		timeTF.setDate(null);//时间
		
		ApplicationContext.listenCard(cardNumTF.getText(), ApplicationContext.CardType.CT_DLSended);
		ApplicationContext.query_member = this;
	}

	@Override
	public void queryMember() {
		termQuery(1);
	}
}
